package com.china08.yxyapi.api.manage.tec.teaching.lecture.res;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.yxyresource.YxyResourceLectureService;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class LectureUploadResController extends YxyApiBaseController {

    @Autowired
    private YxyResourceLectureService yrls;

    @ApiOperation(value = "上传资源")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/res/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws Exception {
        RespModel model = new RespModel();
        YxyProfile profile = getProfile(request, response);
        String userId = profile.getId();
        String schoolId = profile.getSchoolId();
        String provinceCode = profile.getSchoolAreaIdProvince();
        String cityCode = profile.getSchoolAreaIdCity();
        String districtCode = profile.getSchoolAreaIdDistrict();
        String schoolNick = profile.getSchoolName();
        String userNick = profile.getUserNick();
        List<UploadResp> resource = yrls.lectureResourceAdd(reqModel.resources, reqModel.size, reqModel.bookcontentsId,
                reqModel.viewScope, reqModel.intro, userId, schoolId, provinceCode, cityCode, districtCode, schoolNick,
                userNick);
        model.setMessage("上传成功");
        model.resp = resource;
        return model;
    }

    public static class ResourceReqModel {
        public String title;
        public String md5; //
        public String location; // 文件全路径
        public String type; // 类型
        public Long size;
        public String extension;
        public Integer duration;
        public List<Integer> knoledgeList;

        @Override
        public String toString() {
            return "ResourceReqModel [title=" + title + ", md5=" + md5 + ", location=" + location + ", type=" + type
                    + "]";
        }

    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 章节目录
        public String bookcontentsId;
        // 查看范围 0：私有 1：学校 2： 教育云
        public Integer viewScope;
        public String intro;
        // 资源
        public List<ResourceReqModel> resources;

        @Override
        public void autoCheck() {
            Assert.isTrue(viewScope == 0 || viewScope == 1 || viewScope == 2, "非法范围请刷新页面");
            Assert.notNull(StringUtils.defaultIfBlank(bookcontentsId, null), "请选择目录");
            Assert.isTrue(resources != null && resources.size() > 0, "请上传资源");
        }
    }

    public static class UploadResp {
        public String resourceId;
        public String resourceName;
        public String resourceUrl;
        public String formatId;
        public String title;
    }

    protected static class RespModel extends YxyApiRespModel {

        public List<UploadResp> resp;
    }
}
